package mock.action;

import java.util.Map;

import mock.bo.Logging;
import mock.bo.LoggingImp;
import mock.bo.Withdraw;
import mock.bo.WithdrawImp;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class WithdrawAction extends ActionSupport implements SessionAware{
	
	private Withdraw w = new WithdrawImp();
	private Logging logBO = new LoggingImp();
	
	private String btnAction;
	private String txtAmount;
	Map<String, Object> session;
	
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	public String getBtnAction() {
		return btnAction;
	}

	public void setBtnAction(String btnAction) {
		this.btnAction = btnAction;
	}
	
	public String getTxtAmount() {
		return txtAmount;
	}

	public void setTxtAmount(String txtAmount) {
		this.txtAmount = txtAmount;
	}

	/**
	 * gan gia tri cho moi nut bam trong man hinh chon so tien
	 */
	private void setValue(){
		if(txtAmount == null){
			switch(btnAction){
			case "button1": txtAmount = "500000"; break;
			case "button2": txtAmount = "2000000"; break;
			case "button3": txtAmount = "3000000"; break;
			case "button4": txtAmount = "1000000"; break;
			case "button5": txtAmount = "2500000"; break;
			}
		}
	}
	
	/**
	 * thuc hien rut tien
	 */
	@Override
	public String execute() throws Exception {
		setValue();
		double amount;
		int atmID = (Integer)session.get("ATMID");
		String cardNo = (String)session.get("cardNo");
		try{
			amount = Double.parseDouble(txtAmount);
		}catch(Exception e){
			return "error";
		}
		if(amount <= 0) return "error";
		if((amount%50000) != 0){
			return "error";
		}
		boolean check = w.withdraw(cardNo, atmID, amount);
		if(check == true){
			//ghi log
			logBO.logWithdraw(atmID, cardNo, amount);
			//return success
			return "success";
		}
		else return "error";
		//return "test";
	}
	
	

}
